from django.db import models
from django.urls import reverse
from tinymce import models as tinymce_models

from config.models import BaseModel


# Create a category model
class Category(BaseModel):
    # 父级分类
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children',
                               verbose_name='父级分类')
    name = models.CharField(max_length=50, verbose_name='名称')  # 名称
    description = models.TextField(verbose_name='描述')  # 描述
    cover = models.ImageField(upload_to='category', null=True, blank=True, verbose_name='封面')  # 封面

    class Meta:
        verbose_name = 'Category'
        verbose_name_plural = '分类'

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        """
        返回分类的绝对路径
        :return:  分类的绝对路径
        """
        return reverse('category', kwargs={'pk': self.pk})


# Create a blog model
class Article(BaseModel):
    title = models.CharField(max_length=100, verbose_name='标题')  # 标题
    description = models.TextField(null=True, blank=True, verbose_name='描述')  # 描述
    keywords = models.TextField(null=True, blank=True, verbose_name='关键词')  # 关键词
    content = tinymce_models.HTMLField(verbose_name='内容')  # 内容
    click_count = models.IntegerField(default=0, verbose_name='点击数')  # 点击数
    category = models.ForeignKey('Category', on_delete=models.CASCADE, verbose_name='分类')  # 分类
    cover = models.ImageField(upload_to='blog', null=True, blank=True, verbose_name='封面')  # 封面
    image = models.ImageField(upload_to='blog', null=True, blank=True, verbose_name='图片')  # 图片
    status = models.BooleanField(default=True, verbose_name='状态')  # 状态

    class Meta:
        verbose_name = 'Blog'
        verbose_name_plural = '文章'

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        """
        返回文章的绝对路径
        :return:  文章的绝对路径
        """
        return reverse('article_detail', kwargs={'pk': self.pk})


# 创建单页模型
class Page(BaseModel):
    title = models.CharField(max_length=100, verbose_name='标题')  # 标题
    description = models.TextField(null=True, blank=True, verbose_name='描述')  # 描述
    keywords = models.TextField(null=True, blank=True, verbose_name='关键词')  # 关键词
    content = tinymce_models.HTMLField(verbose_name='内容')  # 内容
    cover = models.ImageField(upload_to='page', null=True, blank=True, verbose_name='封面')  # 封面
    slug = models.SlugField(null=True, unique=True, verbose_name='Slug')  # Slug

    class Meta:
        verbose_name = 'Page'
        verbose_name_plural = '单页'

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        """
        返回页面的绝对路径
        :return:  页面的绝对路径
        """
        return reverse('page', kwargs={'slug': self.slug})


# 创建一个模型类，网站设置
class SiteSettings(models.Model):
    site_name = models.CharField(max_length=100, verbose_name='网站名称')
    site_sub_name = models.CharField(max_length=100, verbose_name='网站副标题')
    site_description = models.TextField(verbose_name='网站描述')
    site_keywords = models.TextField(verbose_name='网站关键词')
    site_favicon = models.ImageField(upload_to='favicon/%Y/%m', verbose_name='网站图标')
    site_logo = models.ImageField(upload_to='logo/%Y/%m', verbose_name='网站LOGO')
    site_footer = tinymce_models.HTMLField(verbose_name='网站底部')
    site_address = models.CharField(max_length=100, verbose_name='网站地址')

    class Meta:
        verbose_name = '网站设置'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.site_name


# 创建一个文章评论模型
class Comment(BaseModel):
    article = models.ForeignKey('Article', on_delete=models.CASCADE, related_name='comments', verbose_name='文章')
    content = models.TextField(verbose_name='内容')
    author = models.CharField(max_length=50, verbose_name='用户名')
    email = models.EmailField(verbose_name='邮箱')
    url = models.URLField(verbose_name='网站')
    comment_parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='replies',
                                       verbose_name='父级评论')
    approved = models.BooleanField(default=False, verbose_name='审核')

    class Meta:
        verbose_name = '评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'Comment by {self.author} on {self.article}'

    @property
    def is_parent(self):
        return self.comment_parent is None

    def get_replies(self):
        return self.replies.all()


# 创建一个导航模型
class Navigation(BaseModel):
    name = models.CharField(max_length=50, verbose_name='导航名称')
    url = models.CharField(max_length=50, verbose_name='导航链接')
    sequence = models.IntegerField(default=0, verbose_name='导航排序')

    class Meta:
        verbose_name = '导航'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return self.url
